package org.etlt.load;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.commons.lang3.StringUtils;
import org.etlt.SettingCheck;
import org.etlt.SettingValidationException;

import java.util.ArrayList;
import java.util.List;

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = FileLoaderSetting.class, name = "FILE"),
        @JsonSubTypes.Type(value = DatabaseLoaderSetting.class, name = "DATA_BASE")})
public class LoaderSetting implements SettingCheck {

    public static final int MAX_BATCH_NUMBER = 2000;
    private String name;

    private String extractor;

    private String criteria;

    private boolean autoResolve = false;

    private int batch = 1;

    private String datasource;

    private List<ColumnSetting> columns = new ArrayList<>();

    private List<String> removeColumns = new ArrayList<>();

    private List<String> dependedLoader = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getExtractor() {
        return extractor;
    }

    public void setExtractor(String extractor) {
        this.extractor = extractor;
    }

    public boolean isAutoResolve() {
        return autoResolve;
    }

    public void setAutoResolve(boolean autoResolve) {
        this.autoResolve = autoResolve;
    }

    public List<ColumnSetting> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnSetting> columns) {
        this.columns = columns;
    }

    public List<String> getRemoveColumns() {
        return removeColumns;
    }

    public void setRemoveColumns(List<String> removeColumns) {
        this.removeColumns = removeColumns;
    }

    public String getCriteria() {
        return criteria;
    }

    public void setCriteria(String criteria) {
        this.criteria = criteria;
    }

    public int getBatch() {
        return batch;
    }

    public void setBatch(int batch) {
        this.batch = batch;
    }

    public List<String> getDependedLoader() {
        return dependedLoader;
    }

    public void setDependedLoader(List<String> dependedLoader) {
        this.dependedLoader = dependedLoader;
    }

    public String getDatasource() {
        return datasource;
    }

    public void setDatasource(String datasource) {
        this.datasource = datasource;
    }

    @Override
    public void check() {
        assertName("name", getName());
        if (StringUtils.isBlank(getName()))
            throw new SettingValidationException("missing name.");
        if (!isAutoResolve() && getColumns().isEmpty())
            throw new SettingValidationException("missing column definitions: " + getName());
        if (StringUtils.isEmpty(getExtractor())) {
            throw new SettingValidationException("missing extractors: " + getName());
        }
        if(getBatch() < 0 || getBatch() > MAX_BATCH_NUMBER){
            throw new SettingValidationException("wrong batch set (less than 2000, and greater than 0): " + getBatch());
        }
        if (StringUtils.isBlank(getExtractor()) && StringUtils.isBlank(getCriteria()))
            throw new SettingValidationException("missing criteria while more than 1 extractors found: " + getName());
    }

    @Override
    public String toString() {
        return "LoaderSetting{" +
                "name='" + name + '\'' +
                "depends = " + dependedLoader + '\'' +
                '}';
    }
}
